﻿using SlamDunk.MES.DataDrive.Common.Attributes;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SlamDunk.MES.DataDrive.Common
{
    /// <summary>
    /// 数据驱动菜单
    /// </summary>
    public class DataMenu : INotifyPropertyChanged
    {
        private bool _isExecute;
        /// <summary>
        /// 类的特性信息
        /// </summary>
        public DataTagAttribute TypeDataTag
        {
            get
            {
                return this.TypeInfo.GetCustomAttribute<DataTagAttribute>();
            }
        }

        /// <summary>
        /// 方法的特性信息
        /// </summary>
        public DataTagAttribute MethodDataTag { get; set; }
        /// <summary>
        /// 类信息
        /// </summary>
        public TypeInfo TypeInfo { get; set; }

        /// <summary>
        /// 方法名称
        /// </summary>
        public MethodInfo MethodInfo { get; set; }

        /// <summary>
        /// 备份数据库名称
        /// </summary>
        public string BackDataBaseFileName
        {
            get { return $"{this.MethodDataTag.Sort}-{this.TypeDataTag.DataBaseName}-{this.MethodDataTag.Name}.bak"; }
        }
        /// <summary>
        /// 是否执行
        /// </summary>
        public bool IsExecute
        {
            get { return this._isExecute; }
            set { this._isExecute = value; this.OnPropertyChanged(); this.OnPropertyChanged($"IsExecuteStr"); }
        }

        /// <summary>
        /// 是否执行显示文本
        /// </summary>
        public string IsExecuteStr
        {
            get { return this.IsExecute ? "是" : "否"; }
        }
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="typeInfo">类</param>
        /// <param name="methodInfo">方法</param>
        public DataMenu(TypeInfo typeInfo, MethodInfo methodInfo, DataTagAttribute methodDataTag)
        {
            this.TypeInfo = typeInfo;
            this.MethodInfo = methodInfo;
            this.MethodDataTag = methodDataTag;
        }

        #region # 属性通知实现
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
